//
//  DDComunicatedStore.m
//  DingDongZP
//
//  Created by weily on 16/5/10.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDCommunicatedStore.h"
#import "DDJobfinderDetailInfo.h"
#import "DDJobsBaseModel.h"

NSString *const bossCacheKey = @"bosses";
NSString *const cattleCacheKey = @"cattles";

@interface DDCommunicatedStore ()
{
    YYCache *_cache;
}

@property (nonatomic, strong) YYCache *cache;

@end

@implementation DDCommunicatedStore


+ (instancetype)sharedStore {
    static DDCommunicatedStore *__instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        __instance = [[DDCommunicatedStore alloc] init];
    });
    return __instance;
}

- (void)clearStore {
    [self.cache removeAllObjects];
//    [self.cache setObject:nil forKey:[self cacheKey]];
}

- (YYCache *)cache
{
    if (!_cache) {
        _cache = [YYCache cacheWithName:@"communicated"];
    }
    return _cache;
}

- (NSString *)cacheKey
{
    return @"ckey";
}

- (void)storeCommunicatedBoss:(id)boss
{
    if ([boss isKindOfClass:[DDJobsDetail class]]) {
        DDJobsDetail *bossObj = (DDJobsDetail *)boss;
        
        NSMutableArray *list = (NSMutableArray*)[self.cache objectForKey:bossCacheKey];
        if (!list) {
            list = [NSMutableArray new];
        }
        NSInteger findidx = -1;
        for (DDJobsDetail *bosscache in list) {
            if ([bossObj.jobid isEqualToString:bosscache.jobid]) {
                findidx = [list indexOfObject:bosscache];
            }
        }
        if (findidx != -1) {
            [list removeObjectAtIndex:findidx];
        }
        [list addObject:bossObj];
        
        [self.cache setObject:list forKey:bossCacheKey];
    }
}

- (void)storeCommunicatedCattle:(id)cattle {
    
    if ([cattle isKindOfClass:[DDJobfinderDetailInfo class]]) {
        DDJobfinderDetailInfo *cattleObj = (DDJobfinderDetailInfo *)cattle;
        
        NSMutableArray *list = (NSMutableArray*)[self.cache objectForKey:cattleCacheKey];
        if (!list) {
            list = [NSMutableArray new];
        }
        NSInteger findidx = -1;
        for (DDJobfinderDetailInfo *cattlecache in list) {
            if ([cattleObj.userid isEqualToString:cattlecache.userid]) {
                findidx = [list indexOfObject:cattlecache];
            }
        }
        if (findidx != -1) {
            [list removeObjectAtIndex:findidx];
        }
        [list addObject:cattleObj];
        
        [self.cache setObject:list forKey:cattleCacheKey];
    }
}

- (void)storeObject:(id)obj withKey:(NSString *)key
{
    NSMutableArray *list = (NSMutableArray*)[self.cache objectForKey:key];
    if (!list) {
        list = [[NSMutableArray alloc] init];
    }
    [list addObject:obj];
    [self.cache setObject:list forKey:key];
}

- (NSArray *)storedObjsWithKey:(NSString *)key
{
    return (NSArray *)[self.cache objectForKey:key];
}

- (NSArray *)communicatedBosses {
    return [self storedObjsWithKey:bossCacheKey];
}

- (NSArray *)communicatedCattles {
    return [self storedObjsWithKey:cattleCacheKey];
}

- (void)setJobId:(NSString *)jobid withTargetId:(NSString *)target {
    if (jobid && target) {
        [self.cache setObject:jobid forKey:target];
    }
}

- (NSString *)getJobIdWithTargetId:(NSString *)tagetId {
    return (NSString *)[self.cache objectForKey:tagetId];
}

- (void)setDidChat:(BOOL)did withTarget:(NSString *)targetid {
    NSString *key = [NSString stringWithFormat:@"didchat_%@",targetid];
    [self.cache setObject:@(did) forKey:key];
}

- (BOOL)didChatWithTarget:(NSString *)target {
	NSMutableArray *communicated = (NSMutableArray *)[self.cache objectForKey:@"communicated"];
    if (communicated) {
        return [communicated containsObject:target];
    }
    return NO;
}

- (void)didCommunicatedWithTarget:(NSString *)targetid {
    NSMutableArray *communicated = (NSMutableArray *)[self.cache objectForKey:@"communicated"];
    if (!communicated) {
        communicated = [[NSMutableArray alloc] init];
    }
    else {
        communicated = [communicated mutableCopy];
    }
    
    [communicated removeObject:targetid];
    
    [communicated addObject:targetid];
}


@end
